﻿/**
* @class:drawlineChart(画折线图)
* @date:2008.8.18

*********参数解释
drawlineChart（_txtList,_lineList,_lineColorList,_bgColor）
* @_txtList:折线图文字说明列表
* @_lineList:折线图颜色说明列表
* @_lineColorList:折线图使用颜色列表
* @_bgColor:折线图背景色



@example:
import com._public._display.DrawlineChart

var lineA:Array=new Array("0|123","100|100","200|245","300|120","400|100","500|0","600|0","700|150","800|200","900|300","1000|250");
var lineB:Array=new Array("0|123","100|200","200|100","300|50","400|200","500|100","600|100","700|200","800|300","900|120","1000|300");
var lineC:Array=new Array("0|123","100|0","200|100","300|5","400|0","500|150","600|400","700|230","800|30","900|100","1000|30");
var lineD:Array=new Array("0|123","100|400","200|200","300|300","400|200","500|300","600|100","700|300","800|200","900|300","1000|200");
var lineAry:Array=new Array(lineA,lineB,lineC,lineD);
var txtAry:Array=new Array("A","B","C","D");
var lineColorAry:Array=new Array(0x000000,0xff0000,0x00ff00,0x0000ff);
var bgColorNum:Number=0xffffff;

var dlc:drawlineChart=new drawlineChart(txtAry,lineAry,lineColorAry,bgColorNum);
this.addChild(dlc);*/

package com._public._display{
	import flash.display.Graphics;
	import flash.display.MovieClip;
	import flash.display.Shape;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.geom.Rectangle;
	import flash.text.TextField;
	public class DrawlineChart extends MovieClip {
		public var clickFun:Function;

		private var line:Array;
		private var txtList:Array;
		private var lineColorList:Array;
		private var bgColor:Number;

		private var setX:Number;//折线起始坐标
		private var setY:Number;
		private var shapeBox:Sprite=new Sprite();
		private var maskBox:Sprite=new Sprite();
		private var rect:Shape=new Shape();
		private var hei:Number=400;//波形图高度
		private var wid:Number;
		private var circleList:Array=new Array();
		private var btLeft:Sprite=new Sprite();
		private var btRight:Sprite=new Sprite();
		private var btBox:Sprite=new Sprite();
		private var wordNum:TextField=new TextField();//坐标数据


		public function DrawlineChart(_txtList:Array,_lineList:Array,_lineColorList:Array,_bgColor:Number) {
			line=_lineList;
			txtList=_txtList;
			lineColorList=_lineColorList;
			bgColor=_bgColor;

			var tr:Shape=new Shape();
			var lr:Shape=new Shape();
			var pen:Pen=new Pen(tr.graphics);
			pen.lineStyle(1,0x888888);
			pen.beginFill(0x999999);
			pen.drawTriangle(-20,0,20,20,60);//画等边三角形
			tr.rotation=180;

			var rpen:Pen=new Pen(lr.graphics);
			rpen.lineStyle(1,0x888888);
			rpen.beginFill(0x999999);
			rpen.drawTriangle(0,25,20,20,60);//画等边三角形
			btLeft.addChild(tr);
			btRight.addChild(lr);
			btBox.addChild(btLeft);
			btBox.addChild(btRight);

			btBox.x=690;
			btBox.y=530;
			btBox.rotation=-90;
			btBox.scaleX=0.8;
			btBox.scaleY=0.8;



			for (var i:int=0; i<line.length; i++) {
				for (var j:int=0; j<line[i].length; j++) {

					setX=line[i][j].slice(0,line[i][j].indexOf("|"));
					setY=line[i][j].slice(line[i][j].indexOf("|")+1,line[i][j].length);
					circleList.push([setX,setY]);

					var circle:Shape=new Shape();//第一条折线的所有点
					circle.graphics.beginFill(0x000000);
					circle.graphics.drawCircle(setX,setY,2);
					circle.graphics.endFill();



					var circleSpr:Sprite=new Sprite();
					circleSpr.addChild(circle);

					circleSpr.addEventListener(MouseEvent.MOUSE_OVER,shownum);
					circleSpr.addEventListener(MouseEvent.MOUSE_OUT,hidenum);
					circleSpr.addEventListener(MouseEvent.CLICK,_clickFun);
					shapeBox.addChild(circleSpr);

					var wordNum:TextField=new TextField();
					wordNum.text="("+setX+","+setY+")";
					wordNum.x=setX;
					wordNum.selectable=false;
					wordNum.name="wordNum";
					wordNum.autoSize="left";
					wordNum.y=setY-12;
					wordNum.visible=false;

					var circletxt:Shape=new Shape();
					circletxt.graphics.beginFill(0xffffff);
					circletxt.graphics.drawRect(wordNum.x,wordNum.y,wordNum.width,wordNum.height);
					circletxt.graphics.endFill();

					circleSpr.addChild(wordNum);
					circleSpr.addChildAt(circletxt,0);


				}
			}
			drawLine();//画线
			drawText();


			var scale:Number=shapeBox.height/shapeBox.width;
			shapeBox.height=hei;
			if (hei>400) {
				hei=400;
			}
			btLeft.addEventListener(MouseEvent.MOUSE_DOWN,lclickHandler);
			btRight.addEventListener(MouseEvent.MOUSE_DOWN,rclickHandler);


			this.addChild(maskBox);
			maskBox.x=77;
			maskBox.y=72;

			rect.graphics.beginFill(0x000000);
			rect.graphics.drawRect(0,0,660,506);
			rect.graphics.endFill();
			maskBox.addChild(rect);
			shapeBox.width=hei/scale;
			shapeBox.y=100;
			shapeBox.x=80;
			shapeBox.mask=maskBox;

			this.addChild(shapeBox);
			if (shapeBox.width>rect.width) {
				addChild(btBox);
			}

			var bg:Shape=new Shape();
			bg.graphics.beginFill(bgColor);
			bg.graphics.drawRect(0,0,shapeBox.width,shapeBox.height);
			bg.graphics.endFill();
			shapeBox.addChildAt(bg,0);

			shapeBox.addEventListener(MouseEvent.MOUSE_DOWN,drag);
			shapeBox.addEventListener(MouseEvent.MOUSE_UP,stopdrag);
			//trace(shapeBox.x,rect.x);
		}
		private function drag(e:MouseEvent):void {
			if (shapeBox.width>maskBox.width) {
				var theRec:Rectangle=new Rectangle(-2*rect.width,100,3*rect.width,0);
				shapeBox.startDrag(false,theRec);
			}
		}
		private function stopdrag(e:MouseEvent):void {
			shapeBox.stopDrag();
		}
		private function shownum(e:MouseEvent):void {
			var word:*=e.target;
			if (word is Sprite) {
				word=word.getChildByName("wordNum");
				if (word!=null) {
					word.visible=true;
				}
			}
		}
		private function hidenum(e:MouseEvent):void {
			var word:*=e.target;
			if (word is Sprite) {
				word=word.getChildByName("wordNum");
				if (word!=null) {
					word.visible=false;
				}
			}
		}
		private function _clickFun(e:MouseEvent):void {
			if (clickFun!=null) {
				var word:*=e.target;
				if (word is Sprite) {
					word=word.getChildByName("wordNum");
					if (word!=null) {
						clickFun(word.text);
					}
				}
			}
		}
		private function lclickHandler(e:MouseEvent):void {
			if (shapeBox.x-80<rect.x) {
				shapeBox.x+=50;

			}
		}
		private function rclickHandler(e:MouseEvent):void {
			if (shapeBox.width>rect.width&&rect.width-shapeBox.x<shapeBox.width+20) {
				shapeBox.x-=50;


			}
		}
		private function drawText():void {//线条说明
			var txt:Shape=new Shape();

			for (var colorline:uint=0; colorline<line.length; colorline++) {
				var txtWord:TextField=new TextField();//线条颜色
				txt.graphics.beginFill(lineColorList[colorline]);
				txt.graphics.drawRect(750,100+colorline*30,30,3);
				txt.graphics.endFill();
				txtWord.text=txtList[colorline];//文字
				txtWord.x=790;
				txtWord.y=90+colorline*30;
				addChild(txtWord);
			}
			addChild(txt);
		}
		private function drawLine():void {//折线
			var l:Shape=new Shape();
			for (var k:uint=0; k<line.length; k++) {
				l.graphics.lineStyle(1,lineColorList[k],1);
				l.graphics.moveTo(circleList[line[k].length*k][0],circleList[line[k].length*k][1]);
				//trace("k: "+k+"  moveTo circleList[line.length*k]:  "+line[k].length*k+"坐标  "+circleList[line.length*k][0]+" "+circleList[line.length*k][1]);

				if (k==0) {

					for (var j:uint=0; j<line[k].length-1; j++) {//   0=<j<9
						l.graphics.lineTo(circleList[j+1][0],circleList[j+1][1]);
					}
				} else {
					for (var h:uint=line[k].length*k; h<line[k].length*(k+1)-1; h++) {// k==1时：10<=h<19  ||k==2时：20<=h<29 ……
						l.graphics.lineTo(circleList[h+1][0],circleList[h+1][1]);
					}
				}
				shapeBox.addChild(l);
			}
		}
	}
}
